import {
  createRouter,
  createWebHashHistory,
  
} from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { start, close } from "@/plugins/nprogress";

const modules: any = import.meta.glob("./modules/**/*.ts", { eager: true });
const routeModuleList: RouteRecordRaw[] = [];

Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

const router = createRouter({
  // history: createWebHistory("/demo"),
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/index.vue"),
    },
    {
      path: "/:catchAll(.*)",
      name: "404",
      component: () => import("@/views/error/404.vue"),
    },
    ...routeModuleList,
  ],
});

router.beforeEach((to, from, next) => {
  start();
  next();
});

router.afterEach(() => {
  close();
});

export default router;
